<?php
/**
 * @file
 * @package mileage
 * @version $Id$
**/

if (!defined('XOOPS_ROOT_PATH')) {
	exit;
}

require_once MILEAGE_TRUST_PATH . '/class/AbstractEditAction.class.php';

/**
 * Mileage_RemittanceAction
**/
class Mileage_RemittanceAction extends Mileage_AbstractEditAction
{
	/**
	 * _getId
	 * 
	 * @param   void
	 * 
	 * @return  int
	**/
	protected function _getId()
	{
		return $this->mRoot->mContext->mRequest->getRequest('mile_id');
	}

	/**
	 * &_getHandler
	 * 
	 * @param   void
	 * 
	 * @return  obj Mileage_MileHandler
	**/
	protected function &_getHandler()
	{
		$handler =& $this->mAsset->getObject('handler', 'mile');
		return $handler;
	}

	/**
	 * _setupActionForm
	 * 
	 * @param   void
	 * 
	 * @return  void
	**/
	protected function _setupActionForm()
	{
		$this->mActionForm =& $this->mAsset->getObject('form', 'mile', FALSE, 'edit');// isAdmin:FALSE
		$this->mActionForm->prepare();
	}

	/**
	 * hasPermission
	 * 
	 * @param   void
	 * 
	 * @return  bool
	**/
	public function hasPermission()
	{
		if (Mileage_Utils::getModuleConfig('use_remittance', $this->mAsset->mDirname)) {
			return TRUE;
		} else {
			return FALSE;
		}
	}

	/**
	 * executeViewInput
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewInput(&$render)
	{
		$root =& XCube_Root::getSingleton();
		$uid = $root->mContext->mXoopsUser->get('uid');

		// for remittance LIST
		$mileHandler =& $this->_getHandler();
		$mileCriteria = new CriteriaCompo('1', '1');
		$mileCriteria->add(new Criteria('url', 'remittance:%', 'LIKE'));
		$mileCriteria->add(new Criteria('uid', $uid));
		$mileCriteria->setSort('reg_unixtime', 'DESC');
		$mileObjects =& $mileHandler->getObjects($mileCriteria, 30);// TODO pagenavi
		foreach ($mileObjects as $key => $mObj) {
			$mile_id = $mObj->get('mile_id');
			list(, $remit_uid) = explode(':', $mObj->get('url'));
			$recHandler =& $this->mAsset->getObject('handler', 'rec');
			$recCriteria = new CriteriaCompo('1', '1');
			$recCriteria->add(new Criteria('mile_id', intval($mile_id)));
			$recCriteria->add(new Criteria('uid', intval($remit_uid)));
			$recObject =& $recHandler->getObjects($recCriteria);
			if (!empty($recObject)) {
				$mileObjects[$key]->status = TRUE;
			}
		}

		$dirname = $this->mAsset->mDirname;

//if (function_exists('my_output')) my_output(__FILE__,__LINE__,$this->mActionForm);//my_exit();//NE+

		$render->setTemplateName($dirname . '_remittance.html');
		$render->setAttribute('actionForm', $this->mActionForm);
	//	$render->setAttribute('object', $this->mObject);
		$render->setAttribute('remitedObjects', $mileObjects);
		$render->setAttribute('dirname', $dirname);
		$render->setAttribute('totalPoint', Mileage_Utils::totalMileByUid($dirname));
		$render->setAttribute('uid', $uid);
		$render->setAttribute('xoops_pagetitle', _MD_MILEAGE_LANG_REMITTANCE . ' : ' . _MD_MILEAGE_LANG_MILEAGE);
		$render->setAttribute('xoops_breadcrumbs', Mileage_Utils::breadCrumbs($dirname, _MD_MILEAGE_LANG_REMITTANCE));

		$config_handler =& xoops_gethandler('config');
		$xoopsConfigUser =& $config_handler->getConfigsByDirname('user');
		$render->setAttribute('maxuname', intval($xoopsConfigUser['maxuname']));

		// CSS
		$css = XOOPS_URL . '/modules/' . $dirname . '/style.css';
		$moduleHeader = $render->getAttribute('xoops_module_header');
		$moduleHeader .= '<link rel="stylesheet" type="text/css" media="screen" href="'. $css .'" />';
		$render->setAttribute('xoops_module_header', $moduleHeader);
	}

	/**
	* executeViewSuccess
	* 
	* @param   XCube_RenderTarget  &$render
	* 
	* @return  void
	**/
	public function executeViewSuccess(/*** XCube_RenderTarget ***/ &$render)
	{
		$dirname = $this->mAsset->mDirname;

		// The point is subtracted from the user who remits. TODO :-P
		$db =& $this->mRoot->mController->getDB();
		$sql = sprintf(
				'INSERT INTO `%s` (`point`, `mile_id`, `uid`, `reg_unixtime`) VALUES (%d, %d, %d, %d)',
					$db->prefix($dirname . '_rec'),
					intval($this->mObject->get('point')) * -1,
					intval($this->mObject->get('mile_id')),
					intval($this->mObject->get('uid')),
					time()
			);

		if ($db->query($sql)) {
			$access_uri = $this->mRoot->mContext->mRequest->getRequest('access_uri');
			if (!empty($access_uri)) {
				if (! Mileage_Utils::checkURI($access_uri)){
					$access_uri = XOOL_URL . '/';
				}
			} else {
				$access_uri = Mileage_Utils::makeUri($dirname, 'Remittance');
			}
			$this->mRoot->mController->executeForward($access_uri);
		} else {
			$this->mRoot->mController->executeRedirect($access_uri, 2, 
				_MD_MILEAGE_ERROR_DBUPDATE_FAILED . '(REMITTANCE: error of subtracting point)');
		}
	}

	/**
	 * executeViewError
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewError(/*** XCube_RenderTarget ***/ &$render)
	{
		$access_uri = $this->mRoot->mContext->mRequest->getRequest('access_uri');
		if (!empty($access_uri)) {
			if (! Mileage_Utils::checkURI($access_uri)){
				$access_uri = XOOL_URL . '/';
			}
		} else {
			$access_uri = Mileage_Utils::makeUri($this->mAsset->mDirname, 'Remittance');
		}
		$this->mRoot->mController->executeRedirect($access_uri, 1, _MD_MILEAGE_ERROR_DBUPDATE_FAILED);
	}

	/**
	 * executeViewCancel
	 * 
	 * @param   XCube_RenderTarget  &$render
	 * 
	 * @return  void
	**/
	public function executeViewCancel(/*** XCube_RenderTarget ***/ &$render)
	{
	//	$this->mRoot->mController->executeForward('./index.php?action=RecList');
		$this->mRoot->mController->executeForward(Mileage_Utils::makeUri($this->mAsset->mDirname, 'RecList'));
	}

}
